/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compiledriver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;

/**
 *
 * @author Mark
 */
public class Tokenizing {
    public static String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))";
    private String fileName, proofTable;
    private Environment myEnvironment;
    private ListIterator<String> myIterator;
    
    /**
     * Initializes the class using the filename passed in and creates the internal
     * <code>Environment</code> from the file's contents and develops proof table.
     * @param newFile the filepath for the initializing data
     */
    public Tokenizing(String newFile){
        fileName = newFile;
        myEnvironment = createEnvironment();
        proofTable = myEnvironment.proofTable();
    }
    
    /**
     * Optional call for ProofTable.
     * @return String containing full proof table (formatted)
     */
    public String getProofTable(){
        return proofTable;
    }
    /**
     * Creates the <code>Environment</code> by opening the internal fileName,
     * ripping the data from it, delimiting the data exclusively on 
     * "[\\s*\\t\\n\\r]" and then inclusively on ",()", then creating a 
     * <code>ListIterator</code> to pass to the <code>Environment</code> 
     * constructor
     * @return the initialized <code>Environment</code> object
     */
    private Environment createEnvironment(){
        Environment t = null;
        String entireString;
        String[] entireArray;
        StringTokenizer st;
        List<String> entireList = new ArrayList();
        try{
            entireString = ripStringFromFile(fileName);
            entireString.trim();
            entireArray = entireString.split("[\\s*\\t\\n\\r]");
            for(String s: entireArray){
                st = new StringTokenizer(s, ",()", true);
                while(st.hasMoreTokens()){
                    entireList.add(st.nextToken());
                }
            }
            myIterator = entireList.listIterator();
            t = new Environment(myIterator);
        }
        catch (FileNotFoundException e){
            System.out.println("Did not find the file: " + fileName);
            System.out.println("Please check the filepath.");
            System.exit(1);
        }
        return t;
    }
    
    /**
     * Rips the data from the file pointed to by stringFile as a 
     * single <code>String</code>
     * @param stringFile the filename to pull the data from
     * @return the data from the file in a single <code>String</code>
     * @throws FileNotFoundException Thrown if the file pointed to by stringFile 
     * does not exist
     */
    public String ripStringFromFile(String stringFile) throws FileNotFoundException
    {
        File inFile = new File(stringFile);
        int len;
          char[] chr = new char[4096];
          StringBuilder strBuilder = new StringBuilder();
          FileReader reader = null;
          try {
              reader = new FileReader(inFile);
              while ((len = reader.read(chr)) > 0) {
                  strBuilder.append(chr, 0, len);
              }
          }
          catch (IOException ex)
          {
              System.out.println(ex.getMessage() + "BLAH");
          }
          finally {
              try
              {
                reader.close();
              }
              catch (Exception ex)
              {
                  //quietly suppress any exceptions here, not necessary to report
              }
          }
          return strBuilder.toString();
    }
    
    public Environment getEnvironment(){
        return myEnvironment;
    }

    private void printlist(List<String> entireArr) {
        System.out.println("Made it here");
        for(String s : entireArr){
            System.out.println(s);
        }
    }
    
}
